import requests
import re
import json

"""
爬虫思路
（1）数据来源分析
    1.爬什么
        排名，名称，标题，热度
    2.去哪爬
        url在哪（静态）看其加载方式，在源代码中的就是静态的
        查找后，没用找到，所以确定是动态的
        url='https://weibo.com/ajax/statuses/hot_band'
（2）代码实现
    #发送请求
    #获取数据
    #解析数据
    #保存数据
"""


def get_json(url, headers):
    try:
        resp = requests.get(url=url, headers=headers)
        resp.raise_for_status()
        resp.encoding = 'utf-8'
        return resp.json()
    except:
        return None


def parsel_json(json_data):
    rest_list = []
    data = json_data['data']
    band_list = data['band_list']
    for band in band_list:
        dic = {
            'rank': band['rank'],  # 排名
            'word': band['word'],  # 标题
            'row_hot': band.get('raw_hot')  # 热度
        }
        rest_list.append(dic)
    return rest_list


def save_data(data):
    with open('./web_band_list.csv', 'a+') as f:
        for item in data:
            rank = item['rank']
            word = item['word']
            row_hot = item['row_hot']
            f.write(str(rank+1) + word +str(row_hot)+'\n')


def main():
    url = 'https://weibo.com/ajax/statuses/hot_band'
    headers = {
        'User-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36 Edg/112.0.1722.68',
    }
    data = get_json(url, headers)
    data_save=parsel_json(data)
    save_data(data_save)
    # print(type(parsel_json(data)))
    # print(parsel_json(data))


if __name__ == '__main__':
    main()
